<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css//login.css">
    <script src="js/jquery.min.js"></script>
</head>
<body>
    <div class="nav">
        五子棋对战
    </div>
    <div class="login-container">
        <div class="login-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password2">
            </div>
            <div class="row">
                <button id="submit">提交</button>
                
            </div>
        </div>
    </div>
    <script>
        let usernameInput = document.querySelector("#username");
        let passwordInput = document.querySelector("#password");
        let password2Input = document.querySelector("#password2");
        let submitButton = document.querySelector("#submit");

        submitButton.onclick = function() {
            if(username.value == ""){
                alert("请输入用户名!");
                usernameInput.focus();
                return;
            }else if(password.value==""){
                alert("请输入密码");
                passwordInput.focus();
                return;
            }else if(password2.value==""){
                alert("请确认密码");
                password2Input.focus();
                return;
            }
            if (password.value != password2.value){
                alert("密码不一致");
                password.focus();
                return;
            } 
            jQuery.ajax({
                url:"/register",
                type:"POST",
                data:{
                    username: usernameInput.value,
                    password: passwordInput.value
                },
                success:function(data) {
                    //如果注册成功，会返回新的User对象
                    //登录失败，会返回一个空的User对象
                    if(data && data.username!=""){
                        alert("注册成功");
                        location.assign("/login.html");
                    }else{
                        alert("登录失败");
                    }
                },
                error:function(){
                    alert("注册失败");
                }
            })
        }
    </script>
</body>
</html>